package dfsketches.gr3d;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.MemoryImageSource;

import javax.swing.JFrame;

import dfsketches.visualizer_2d.WindowCloser;

public class Window extends JFrame implements FocusListener {

	private static final long serialVersionUID = -1955162665659594417L;

	public Window(int[] forDraw, int w, int h, Controller control) {
		super();

		this.h = h;
		this.w = w;
		bitmap = forDraw;
		
		setName("Plotter");
		setTitle("Terrain Generator");
		setSize(w + 15, h + 40);
		setVisible(true);
		

		image = createImage(new MemoryImageSource(w, h, bitmap, 0, w));
		addWindowListener(new WindowCloser());
		addMouseListener(new MouseRotate(control, x_shift, y_shift));
		addKeyListener(control);
		addFocusListener(this);
	}
	
	public void draw() {
		image = createImage(new MemoryImageSource(w, h, bitmap, 0, w));
		paint(getGraphics());
	}
	
	public void paint(Graphics g) {
		g.drawImage(image, x_shift, y_shift, this);
	}
	
	private int x_shift = 5;
	private int y_shift = 30;
	private Image image;
	private int w;
	private int h;
	private int[] bitmap;

	@Override
	public void focusGained(FocusEvent event) {
		paint(getGraphics());
	}

	@Override
	public void focusLost(FocusEvent event) {
		// TODO Auto-generated method stub
	}
}